<extend name="layout/edit" />

<block name="HEADER">
	<style type="text/css">
		.xw-form .xw_searchs_bodys{
			line-height: 30px;padding: 10px;padding-bottom: 5px;position: relative;
		}
		.file-img {
			height: 100%;
			background-position: left;
			background-size: 100% 85%;
			background-repeat: no-repeat;
			cursor: pointer;
		}
		#selectGoodsGrid .layui-btn{ display: none; }
		.selectBtnRow{
			width: 40px;background: #f2f2f2;text-align: center;position: relative;
		}
		.selectBtnRow-boy{
			position: absolute;top: 50%;margin-top: -50px;width: 100%;
		}
		.selectBtnRow-boy div{
			padding: 5px 0;
		    background: #1e9fff;
		    margin-bottom: 10px;
		    color: #fff;
		}
		.selectBtnRow-boy div:hover{
			color: #0000FF;
			cursor: pointer;
		}
	</style>
</block>

<block name="CONTENT">
	<div id="form" padding="" height="" style="" class="xw-form">
		<div class="wrapper" style="height: calc(100% - 42px);">
			<div class="panel-heading xw_searchs_bodys">
				<control:search id="selectGodcateSearchs" />
			</div>
			<div class="content" style="height: calc(100% - 45px);padding: 5px;display: flex;">
				<div style="width: calc(100%);">
					<control:grid id="selectGoodcateGrid" padding="0" height="100%" />
				</div>
				<!--<div class="selectBtnRow">
					<div class="selectBtnRow-boy" >
						<div onclick="page.add()">>></div>
						<div onclick="page.del()"><<</div>
					</div>
				</div>
				<div style="width: calc(50% - 20px);">
					<control:grid2 id="selectedGoodsGrid" padding="0" height="100%" />
				</div>-->
			</div>
		</div>
		<div class="xw-btnRow" style="text-align: right;">
			<button type="button" class="layui-btn layui-btn-normal layui-btn-sm " id="btnSave" onclick="page.save()" ><i class="iconfont" style="line-height: 0;"></i>确认选择</button> 
			<button type="button" class="layui-btn layui-btn-normal layui-btn-sm " id="close" onclick="$.close()"> <i class="iconfont" style="line-height: 0;"></i>关闭 </button>
		</div>
	</div>

</block>

<block name="FOOTER">
	<script type="text/javascript">
		var page = (function(ns) {
			
			ns.selectedIds = [];
			
			ns.init = function(){

                //页面传值，选择了，就不显示。
                var filter=[{"field":"id","opera":"not in","value":$.getRequest("id")}];
                selectGoodcateGrid.load(filter);

			//	ns.query();
			}
			
			//搜索事件
		    ns.query=function(){

                selectGoodcateGrid.load(selectGodcateSearchs.getData());
		    }
			
			//添加
			ns.add = function(){
				var ids = $.map(selectGoodcateGrid.getSelectedRows(),function(h){
					return h.id;
				});
				var ids2 = $.map(selectGoodcateGrid.getRows(),function(h){
					return h.id;
				})
				
				if(!ids[0]){
					$.toast(0,"请选择需要添加的商品");
					return false;
				}
				ns.selectedIds = $.uniques(ns.selectedIds.concat(ids));
				ns.selectedIds = $.uniques(ns.selectedIds.concat(ids2));
				var oids = '';	
				if($.getRequest('type') != 2){
					oids = ns.selectedIds;
				}else{
					oids = $.uniques(ids.concat(ids2));
				}
                selectGoodcateGrid.load([{"field":"id","opera":"in","value": oids.join(',') }]);
				ns.query();
			}
			
			//删除
			ns.del = function(){
				var ids = $.map(selectGoodcateGrid.getSelectedRows(),function(h){
					return h.id;
				});
				if(!ids[0]){
					$.toast(0,"请选择需要移除的商品");
					return false;
				}
				
				ns.selectedIds = $.map(ns.selectedIds,function(id){
					if( ids.indexOf(id) == -1 && ids.indexOf( Number(id) ) == -1 ){
						return id;
					}
				});

                selectGoodcateGrid.load([{"field":"id","opera":"in","value": ns.selectedIds.join(',') }]);
				ns.query();
			}
			
			//搜索事件
			ns.save = function() {
//				var ids = selectedGoodsGrid.getAllColumnValue('id'); 
//				var datas = selectedGoodsGrid.getRows(); 
				var ids = selectGoodcateGrid.getSelectedColumnValue('id');
				var datas = selectGoodcateGrid.getSelectedRows();
				if(!ids){
					$.toast(0,"请先选择分类！");
					return false;
				}
				if( parent.page && parent.page.selectShopsCallback){
					if(parent.page.selectShopsCallback(ids,datas) !== false){
						$.close();
					}
				}else{
					$.close();
				}
			}

			return ns;
		}({}));

		$(function() { page.init(); });
	</script>
</block>